package com.solarStruggle.util

/**
 * A two-dimensional vector
 */
class Vector2D(val dx: Double, val dy: Double) {
  
  lazy val magnitude: Double = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2))
  
  def normalize(): Vector2D = if(magnitude != 0.0d) new Vector2D(dx / magnitude, dy / magnitude) else new Vector2D(0, 0)
  
  def +(other: Vector2D): Vector2D = new Vector2D(dx + other.dx, dy + other.dy)
  
  def -(other: Vector2D): Vector2D = new Vector2D(dx - other.dx, dy - other.dy)
  
  def *(value: Double): Vector2D = new Vector2D(dx * value, dy * value)
  
  def /(value: Double): Vector2D = if(value != 0.0d) new Vector2D(dx / value, dy / value) else new Vector2D(0.0d, 0.0d)
  
  override def toString(): String = "(" + dx + ", " + dy + ")[" + magnitude + "]"
}
